package com.syh.app.basic.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.launcher.ARouter;
import com.gyf.immersionbar.ImmersionBar;
import com.syh.app.basic.R;
import com.syh.app.basic.base.view_model.BaseMldEvent;
import com.syh.app.basic.base.view_model.BaseViewModel;
import com.syh.app.basic.utils.ActivityManager;
import com.syh.app.basic.utils.ComUtil;
import com.syh.app.basic.views.LoadingDialog;
import com.trello.rxlifecycle2.android.ActivityEvent;
import com.trello.rxlifecycle2.components.support.RxAppCompatActivity;

/**
 * 创建者：SYH
 * 时间：2019/9/26 16:47
 * 编辑者：SYH、
 * 补充说明：activity基类
 */
public abstract class BaseActivity<VDB extends ViewDataBinding, VM extends BaseViewModel<ActivityEvent>> extends RxAppCompatActivity {

    public static final String SHOW_LOADING = "show_loading";
    public static final String HIDE_LOADING = "hide_loading";
    public static final String FINISH = "finish";
    public static final String FINISH_WITH_RESULT = "finish_with_result";

    protected VDB mVDBinding;
    protected VM mViewModel;
    private LoadingDialog mLoadingDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManager.getInstance().pushOneActivity(this);
        if (needImmersionBarInit())
            ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.bg3).statusBarDarkFont(true).init();
        mVDBinding = DataBindingUtil.setContentView(this, getContentLayout());
        mViewModel = (VM) new ViewModelProvider(getViewModelStore(), ViewModelProvider.AndroidViewModelFactory.getInstance(this.getApplication())).get(ComUtil.getClassT(BaseActivity.this, 1));
        mVDBinding.setVariable(getViewModelId(), mViewModel);
        mViewModel.initLifecycleProvider(this);
        getLifecycle().addObserver(mViewModel);
        //注册
        ARouter.getInstance().inject(this);
        initIntentData();
        setTitleViewModel();
        initView(savedInstanceState);
        initObservers();
        initData();
    }

    @Override
    protected void onDestroy() {
        if (mVDBinding != null) {
            mVDBinding.unbind();
        }
        ActivityManager.getInstance().popOneActivity(this);
        getLifecycle().removeObserver(mViewModel);
        super.onDestroy();
    }


    protected abstract int getContentLayout();//获取内容布局

    protected abstract int getViewModelId();//获取ViewModel的BR ID

    //初始化Intent里的数据
    protected void initIntentData() {
    }

    //初始化titleViewModel
    protected void setTitleViewModel() {
    }

    protected abstract void initView(Bundle savedInstanceState);//初始化界面控件

    protected void initObservers() {
        mViewModel.getBaseMld().observe(this, new Observer<BaseMldEvent>() {
            @Override
            public void onChanged(BaseMldEvent baseMldEvent) {
                switch (baseMldEvent.action) {
                    case SHOW_LOADING:
                        showLoading(baseMldEvent.stringMsg);
                        break;
                    case HIDE_LOADING:
                        hideLoading(baseMldEvent.stringMsg);
                        break;
                    case FINISH:
                        finish();
                        break;
                    case FINISH_WITH_RESULT:
                        setResult(baseMldEvent.intMsg);
                        finish();
                        break;
                }
            }
        });
    }

    protected void initData() {
    }

    protected boolean needImmersionBarInit() {
        return true;
    }

    protected void showLoading(String tag) {
        if (mLoadingDialog == null) {
            mLoadingDialog = new LoadingDialog(this);
        }
        mLoadingDialog.showDialog(tag);
    }

    protected void hideLoading(String tag) {
        if (mLoadingDialog != null) {
            mLoadingDialog.hideDialog(tag);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        //重新启动服务   手机经常会关闭掉app service
//        if (!App.getApplication().isServiceAlive() && !this.getClass().getSimpleName().equals("SplashActivity")) {
//            try {
//                startService(new Intent(this, WsService.class));
//                App.getApplication().setServiceAlive(true);
//            } catch (Exception e) {
//                Loog.e("BaseActivity", e.getMessage());
//            }
//        }
    }
}
